package com.ymatou.iapi.openapibiz.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.openapibiz.parameter.AuthorizeBean;
import com.ymatou.iapi.openapibiz.parameter.CancelAuthBean;
import com.ymatou.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.ymatou.iapi.openapibiz.service.AuthorizeCall;
import com.ymatou.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OpenApiBizCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*授权卖手给应用授权app
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Authorize {

	private static AuthorizeBean authorizeBean;
	private static AuthorizeCall authorizeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("授权卖手给应用授权app");
	}

	@Before
	public void caseUp() {
		authorizeBean = new AuthorizeBean();
		authorizeCall = new AuthorizeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(authorizeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_001() {
		Logger.start(true, "授权-自主开发");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(TestDataManager.getRandomIntNum(6));
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");
			//			authorizeBean.setAppDesc("APP_Desc "+TestDataManager.getRandomIntNum(10));

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					appName).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("USER_DEV", application.getType(), "验证数据库type");
			Logger.verifyEquals(0, application.getIs_shenqi(), "验证数据库is_shenqi");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_002() {
		Logger.start(true, "授权-第三方应用");
		try {
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();

			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService
					.createNewThirdPartyAppBean();
			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService
					.newthirdpartyappCall(newThirdPartyAppBean);

			Integer userId = TestDataManager.getRandomIntNum(10);

			authorizeBean.setSellerId(userId);
			authorizeBean.setAppId(newThirdPartyAppCall.getAppId());
			authorizeBean.setType("THIRD_PARTY");
			authorizeBean.setAppDesc("2222");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					newThirdPartyAppBean.getAppName()).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("THIRD_PARTY", application.getType(),
					"验证数据库type");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(0, application.getIs_shenqi(), "验证数据库is_shenqi");
			Logger.verifyEquals(newThirdPartyAppBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_003() {
		Logger.start(true, "sellerId不传");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals("400", authorizeCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_004() {
		Logger.start(true, "sellerId传0");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(0);
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_005() {
		Logger.start(true, "appId传空");
		try {
			Integer userId = TestDataManager.getRandomIntNum(10);

			authorizeBean.setSellerId(userId);
			authorizeBean.setType("THIRD_PARTY");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals("400", authorizeCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_006() {
		Logger.start(true, "appName传空");
		try {
			authorizeBean.setSellerId(3383);
			authorizeBean.setType("USER_DEV");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals("400", authorizeCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_007() {
		Logger.start(true, "type传空");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(TestDataManager.getRandomIntNum(6));
			authorizeBean.setAppName(appName);

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals("400", authorizeCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_008() {
		Logger.start(true, "appId不存在");
		try {
			Integer userId = TestDataManager.getRandomIntNum(10);

			authorizeBean.setSellerId(userId);
			authorizeBean.setAppId("wwww");
			authorizeBean.setType("THIRD_PARTY");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals("400", authorizeCall.getCode(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_010() {
		Logger.start(true, "type不存在");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(TestDataManager.getRandomIntNum(6));
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV22");

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_011() {
		Logger.start(true, "授权-自主开发的应用取消授权后还能创建相同名字的应用授权");
		try {
			OpenApiWapper openApiWapper = new OpenApiWapper();

			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(TestDataManager.getRandomIntNum(6));
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");
			authorizeBean.setAppDesc("APP_Desc "
					+ TestDataManager.getRandomIntNum(10));

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Application application = openApiWapper.selectApplicationByAppName(
					appName).get(0);
			String appId1 = application.getApp_id();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(appId1)
					.get(0);

			// 取消授权
			OpenApiBizCallService service = new OpenApiBizCallService();
			CancelAuthBean cancelAuthBean = service.createCancelAuthBean(
					authorizeBean.getSellerId(), authCode.getAuth_code(),
					appId1);
			service.cancelAuth(cancelAuthBean);

			// 再次对相同名称的应用授权			
			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			Application application2 = openApiWapper
					.selectApplicationByAppName(appName).get(0);
			AuthCode authCode2 = openApiWapper.selectAuthCodeByAppId(
					application2.getApp_id()).get(0);

			Application oldApplication = new OpenApiWapper()
					.selectApplicationById(appId1).get(0);

			Logger.verifyEquals("USER_DEV", oldApplication.getType(),
					"验证数据库type");
			Logger.verifyEquals("DISABLE", oldApplication.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					oldApplication.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

			Logger.verifyEquals("USER_DEV", application2.getType(), "验证数据库type");
			Logger.verifyEquals("ENABLE", application2.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					application2.getApp_desc(), "验证数据库App_desc一致");

			int day2 = (int) ((authCode2.getExpire_time().getTime() - authCode2
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day2 > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_012() {
		Logger.start(true, "授权-第三方应用");
		try {
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();

			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService
					.createNewThirdPartyAppBean();
			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService
					.newthirdpartyappCall(newThirdPartyAppBean);

			Integer userId = TestDataManager.getRandomIntNum(10);

			authorizeBean.setSellerId(userId);
			authorizeBean.setAppId(newThirdPartyAppCall.getAppId());
			authorizeBean.setType("THIRD_PARTY");
			authorizeBean.setAppDesc(newThirdPartyAppBean.getAppDesc());
			authorizeBean.setAppName(newThirdPartyAppBean.getAppName());

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					newThirdPartyAppBean.getAppName()).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("THIRD_PARTY", application.getType(),
					"验证数据库type");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_013() {
		Logger.start(true, "授权-自主开发");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			int sellerId = Integer.valueOf(24635724);

			authorizeBean.setSellerId(sellerId);
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");
			//			authorizeBean.setAppDesc("APP_Desc "+TestDataManager.getRandomIntNum(10));

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					appName).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("USER_DEV", application.getType(), "验证数据库type");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_014() {
		Logger.start(true, "授权-自主开发,神器用户");
		try {
			String appName = "auto_USER_DEV_"
					+ TestDataManager.getRandomIntNum(6);

			authorizeBean.setSellerId(500860472);
			authorizeBean.setAppName(appName);
			authorizeBean.setType("USER_DEV");
			authorizeBean.setShenqi(true);
			//			authorizeBean.setAppDesc("APP_Desc "+TestDataManager.getRandomIntNum(10));

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					appName).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("USER_DEV", application.getType(), "验证数据库type");
			Logger.verifyEquals(1, application.getIs_shenqi(), "验证数据库is_shenqi");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(authorizeBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Authorize_015() {
		Logger.start(true, "授权-第三方应用,神器用户");
		try {
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();

			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService
					.createNewThirdPartyAppBean();
			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService
					.newthirdpartyappCall(newThirdPartyAppBean);

			Integer userId = TestDataManager.getRandomIntNum(10);
			
			authorizeBean.setSellerId(userId);
			authorizeBean.setAppId(newThirdPartyAppCall.getAppId());
			authorizeBean.setType("THIRD_PARTY");
			authorizeBean.setAppDesc("2222");
			authorizeBean.setShenqi(true);

			authorizeCall.setData(authorizeBean);
			authorizeCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, authorizeCall.getCode(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					newThirdPartyAppBean.getAppName()).get(0);
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(
					application.getApp_id()).get(0);

			Logger.verifyEquals("THIRD_PARTY", application.getType(),
					"验证数据库type");
			Logger.verifyEquals("ENABLE", application.getStatus(),
					"验证数据库Status");
			Logger.verifyEquals(1, application.getIs_shenqi(), "验证数据库is_shenqi");
			Logger.verifyEquals(newThirdPartyAppBean.getAppDesc(),
					application.getApp_desc(), "验证数据库App_desc一致");

			int day = (int) ((authCode.getExpire_time().getTime() - authCode
					.getCreate_time().getTime()) / (1000 * 60 * 60 * 24));
			Logger.verifyEquals(true, day > 364, "验证数据库ExpireTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}